﻿using UnityEngine;
using System.Collections;

public class Character_Player : MonoBehaviour {
	
	public float runSpeed;
	//the character's basic movement speed.

	public float acceleration;
	//how quickly the character reaches it's target speed.

	public Character_Controller cc;
	[HideInInspector]
	public Character_Stab characterMelee;
	
	[HideInInspector]
	public bool invulnerable;
	[HideInInspector]
	public Vector3 dir;

//	[HideInInspector]
//	public bool running;
	[HideInInspector]
	public bool attacking;
	[HideInInspector]
	public float hitBoxSize;
	[HideInInspector]
	public Vector3 attackPoint;
	
	[HideInInspector]
	public Vector3[] speed=new Vector3[2];

	float rotSpeed=11.5f;
	[HideInInspector]
	public Quaternion rotTarget;
	[HideInInspector]
	public float rotAngle;

	public Animator anim;                          // a reference to the animator on the character

	public bool HasHair;

	public GameObject Mouth;
	public GameObject Knife;
	public GameObject Tablet;
	public GameObject Hair;
	
	[HideInInspector]
	public bool Dead;
	
	void Awake () {
		SetRotAngle(transform.position+(Vector3.forward*-10f));
		SetRotTarget();
		
		transform.rotation=rotTarget;
		
		characterMelee=GetComponent<Character_Stab>();
		Knife.renderer.enabled = false;
		Mouth.renderer.enabled = false;
		Tablet.renderer.enabled = false;
		if(!HasHair) {
				Hair.renderer.enabled = false;
		}
	}

	void Update () {
		if(Dead) { return; }
		UpdateMovement();
	}

	void FixedUpdate(){
		if(Dead) { return; }
		UpdateMovementFixed();	
	}

	void UpdateMovement(){
		transform.rotation=Quaternion.Slerp (transform.rotation,rotTarget,(Time.deltaTime*rotSpeed));
		
		speed[0]=Vector3.MoveTowards(speed[0],speed[1],(Time.deltaTime*acceleration));
		if (float.IsNaN (speed [0].x))
			speed [0] = Vector3.zero; // NaNs showed up occasionally.
									//Once this starts, it self-perpetuates ... unless we stop it.
	}
	
	void UpdateMovementFixed(){
		//cc.Move(speed[0]*Time.deltaTime);
		rigidbody.MovePosition(new Vector3(rigidbody.transform.position.x,0f,rigidbody.transform.position.z)+(speed[0]*Time.deltaTime));
	}
	
	public void SetRotAngle(Vector3 destination){
		dir=(destination-new Vector3(transform.position.x,0f,transform.position.z));
		float distTotal=dir.magnitude;
		dir/=distTotal;
		
		rotAngle=GetAngle(	new Vector2(transform.position.x,transform.position.z),
		                  new Vector2(destination.x,destination.z));
		rotAngle=(-rotAngle+90f);
	}
	
	public static float GetAngle(Vector2 startPos, Vector2 targetPos){
		float angle=(Mathf.Atan2((targetPos.y-startPos.y),(targetPos.x-startPos.x)))*(180f/Mathf.PI);
		return angle;
	}
	
	public void SetRotTarget(){
		rotTarget=Quaternion.Euler(new Vector3(0f,rotAngle,0f));
	}

	public void AttackStart(){
		characterMelee.AttackStart();
	}
	
	////////////////
	//DAMAGE
	////////////////
	
	public void AttackCancel(){
		attacking=false;		
		characterMelee.AttackCancel();	
	}

	private void Damage(string relatedAnim, int layer) {
		cc.AnimateViaNetwork (relatedAnim, layer);
		speed[1]=Vector3.zero;
	} // end method

	public void Hurt(){
		Damage("Hurt", 1);
		invulnerable=cc.StartInvulnerability();
	}

	public void Kill(){
		Damage("Death", 0);

		cc.controlActive=false;
		Dead = true;
		collider.enabled = false;

		GameObject[] targets = GameObject.FindGameObjectsWithTag("Player");
		foreach (GameObject t in targets) {
			Debug.Log("Loop through laughers.");

			Character_Player c = t.GetComponent<Character_Player> ();
			if (!c.Dead)
				c.cc.TransmitLaughSignal();
		}
	}
	
}
